import java.io.*;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


/**
 * Class to parse the XML file passed by a client to
 * the server.
 * @author Marco Vellinga
 * @author Sander Wagenaar
 * @author http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
 */
 public class SAXparser {
	 
	 // Variables
	 InputStream input;
	 ArrayList<Data> allData = new ArrayList<Data>();
	 Data data = new Data();
	 
	 public SAXparser(InputStream input){
		
		 this.input = input;

		try {
			
			// Setup a new parser
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			
			
			// Setup a new handler
			DefaultHandler handler = new DefaultHandler() {
			
			// Set tags to false
			boolean stn = false;
			boolean date = false;
			boolean time = false;
			boolean temp = false;
			boolean dewp = false;
			boolean stp = false;
			boolean slp = false;
			boolean visib = false;
			boolean wdsp = false;
			boolean prcp = false;
			boolean sndp = false;
			boolean frshtt = false;
			boolean cldc = false;
			boolean wnddir = false;
			
			/**
			 * Function to check if the element the equals the ones we
			 * are looking for
			 */
			public void startElement(String uri, String localName,String qName, 
		                Attributes attributes) throws SAXException {
				
				if (qName.equalsIgnoreCase("STN")) {
					stn = true;
				}
	
				if (qName.equalsIgnoreCase("DATE")) {
					date = true;
				}
		 
				if (qName.equalsIgnoreCase("TIME")) {
					time = true;
				}
		 
				if (qName.equalsIgnoreCase("TEMP")) {
					temp = true;
				}
				
				if (qName.equalsIgnoreCase("DEWP")) {
					dewp = true;
				}
				
				if (qName.equalsIgnoreCase("STP")) {
					stp = true;
				}
				
				if (qName.equalsIgnoreCase("SLP")) {
					slp = true;
				}
				
				if (qName.equalsIgnoreCase("VISIB")) {
					visib = true;
				}
				
				if (qName.equalsIgnoreCase("WDSP")) {
					wdsp = true;
				}
				
				if (qName.equalsIgnoreCase("PRCP")) {
					prcp = true;
				}
				
				if (qName.equalsIgnoreCase("SNDP")) {
					sndp = true;
				}
				
				if (qName.equalsIgnoreCase("FRSHTT")) {
					frshtt = true;
				}
				
				if (qName.equalsIgnoreCase("CLDC")) {
					cldc = true;
				}
				
				if (qName.equalsIgnoreCase("WNDDIR")) {
					wnddir = true;
				}
		 
			}
			
			/**
			 * Function to check the end of an element. If the elements
			 * measurement ends add all the data to the arraylist and
			 * make a fresh set of data
			 */
			public void endElement(String uri, String localName,
				String qName) throws SAXException {
				
				if (qName.equalsIgnoreCase("MEASUREMENT")) {
					allData.add(data);
					data = new Data();
				}
			}
			
			/**
			 * Function to get the data from the starting elements we
			 * need and put them in the data object
			 */
			public void characters(char ch[], int start, int length) throws SAXException {
		 
				if (stn) {
					data.setStn(new String(ch, start, length));
					stn = false;
				}
		 
				if (date) {
					data.setDate(new String(ch, start, length));
					date = false;
				}
		 
				if (time) {
					data.setTime(new String(ch, start, length));
					time = false;
				}
		 
				if (temp) {
					data.setTemp(new String(ch, start, length));
					temp = false;
				}
				
				if (dewp) {
					data.setDewp(new String(ch, start, length));
					dewp = false;
				}
				
				if (stp) {
					data.setStp(new String(ch, start, length));
					stp = false;
				}
				
				if (slp) {
					data.setSlp(new String(ch, start, length));
					slp = false;
				}
				
				if (visib) {
					data.setVisib(new String(ch, start, length));
					visib = false;
				}
				
				if (wdsp) {
					data.setWdsp(new String(ch, start, length));
					wdsp = false;
				}
				
				if (prcp) {
					data.setPrcp(new String(ch, start, length));
					prcp = false;
				}
				
				if (sndp) {
					data.setSndp(new String(ch, start, length));
					sndp = false;
				}
				
				if (frshtt) {
					data.setFrshtt(new String(ch, start, length));
					frshtt = false;
				}
				
				if (cldc) {
					data.setCldc(new String(ch, start, length));
					cldc = false;
				}
				
				if (wnddir) {
					data.setWnddir(new String(ch, start, length));
					wnddir = false;
				}
		 
			}
		 
		     };
		     
		     // Parse the file using the defined handler
		     InputSource source = new InputSource(input);
		     saxParser.parse(source, handler);
	       
	 
	     } catch (Exception e) {
	       e.printStackTrace();
	     }
	 
   }
	
	/**
	 * Function to return the data object used by the parser
	 * @return
	 */
	public synchronized ArrayList<Data> getData(){
		return allData;
	}
	
	
 
}